package com.yu.mail.starter.configs;

import com.yu.mail.starter.properties.MailProperties;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

/**
 * 邮件发送类
 * @author yuhh
 * @date 2021-05-24 18:06
 **/
public class MailSender {

    private MailProperties properties;

    /**
     * 建立邮件发送器
     * @return  org.springframework.mail.javamail.JavaMailSenderImpl 配置好的邮件发送器
     * @author yuhh
     * @date 2019/11/27 16:37
     */
    public JavaMailSenderImpl createMailSender() {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost(properties.getMailHost());
        sender.setPort(properties.getMailPort());
        sender.setUsername(properties.getMailUserName());
        sender.setPassword(properties.getMailPassword());
        sender.setDefaultEncoding("Utf-8");
        Properties p = new Properties();
        p.setProperty("mail.smtp.timeout", properties.getMailTimeout());
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.smtp.ssl.enable", "true");
        p.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        sender.setJavaMailProperties(p);
        return sender;
    }

    public MailSender(MailProperties properties){
        this.properties = properties;
    }

    public MailSender(){}
}
